package com.pj.compose.ui.compose

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.pj.compose.ui.theme.c666666

@Composable
fun SimpleListView(
    items: List<String>,
    fontSize: TextUnit = 16.sp,
    textColor: Color = c666666,
    useLoadMore: Boolean = false,
    hasMore:Boolean = false,
    onLoadMore: () -> Unit = {},
    onItemClick: (Int) -> Unit
) {
    val listState = rememberLazyListState()
    if (useLoadMore && hasMore)
        LoadMoreListHandler(listState = listState, onLoadMore = onLoadMore)
    LazyColumn(state = listState) {
        items.forEachIndexed { index, s ->
            item {
                Column(
                    Modifier.clickable { onItemClick(index) }) {
                    Text(
                        text = s,
                        modifier = Modifier.padding(15.dp),
                        fontSize = fontSize,
                        color = textColor
                    )
                    Divider()
                }
            }
        }
        if (useLoadMore && items.isNotEmpty())
            item {
                Text(
                    text = if (hasMore) "加载更多" else "没有了",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(15.dp),
                    textAlign = TextAlign.Center
                )
            }

    }
}

@Preview(showBackground = true)
@Composable
private fun SimpleListViewPrev() {
    SimpleListView(listOf("item1", "item2", "item3", "item4", "item5", "item6")) {}
}